package com.caozhi.mspeoject.nb_search.controller;

import com.caozhi.mspeoject.nb_search.remote.InsuranceRemote;
import com.caozhi.mspeoject.nb_search.repository.PolicySearchRepository;
import com.woniuxy.boot.common.bean.EsPolicy;
import com.woniuxy.boot.common.bean.Insurance;
import com.woniuxy.boot.common.dto.EsPolicySearchDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("policySearch")
public class PolicySearchController {
    @Autowired
    private PolicySearchRepository searchRepository;
    @Autowired
    private InsuranceRemote insuranceRemote;

    @PostMapping("get")
    public List<EsPolicy> getPolicy(@RequestBody EsPolicySearchDTO esPolicySearchDTO) {
        Insurance insurance = insuranceRemote.getInsuranceById(esPolicySearchDTO.getInsuranceId());
        List<EsPolicy> policyList = searchRepository.findByNoOrNameOrPhoneOrFrameNumberOrCarNumber(esPolicySearchDTO.getKeyword(), esPolicySearchDTO.getKeyword(), esPolicySearchDTO.getKeyword(), esPolicySearchDTO.getKeyword(), esPolicySearchDTO.getKeyword());
        if (esPolicySearchDTO.getInsuranceId() != 0) {
            policyList = policyList.stream().filter(p -> p.getInsurances().contains(insurance)).toList();
        }
        if (esPolicySearchDTO.getPolicyStatus() != null) {
            policyList = policyList.stream().filter(p -> p.getStatus().equals(esPolicySearchDTO.getPolicyStatus())).toList();
        }

        if (esPolicySearchDTO.getStartTime() != null && esPolicySearchDTO.getNextStartTime() != null) {
            policyList = policyList.stream().filter(p -> p.getStartTime().isAfter(esPolicySearchDTO.getStartTime()) && p.getStartTime().isBefore(esPolicySearchDTO.getNextStartTime())).toList();
        } else if (esPolicySearchDTO.getStartTime() != null) {
            policyList = policyList.stream().filter(p -> p.getStartTime().isAfter(esPolicySearchDTO.getStartTime())).toList();
        } else if (esPolicySearchDTO.getNextStartTime() != null) {
            policyList = policyList.stream().filter(p -> p.getStartTime().isBefore(esPolicySearchDTO.getNextStartTime())).toList();
        }

        if (esPolicySearchDTO.getEndTime() != null && esPolicySearchDTO.getNextEndTime() != null) {
            policyList = policyList.stream().filter(p -> p.getEndTime().isAfter(esPolicySearchDTO.getEndTime()) && p.getEndTime().isBefore(esPolicySearchDTO.getNextEndTime())).toList();
        } else if (esPolicySearchDTO.getEndTime() != null) {
            policyList = policyList.stream().filter(p -> p.getEndTime().isAfter(esPolicySearchDTO.getEndTime())).toList();
        } else if (esPolicySearchDTO.getNextEndTime() != null) {
            policyList = policyList.stream().filter(e -> e.getEndTime().isBefore(esPolicySearchDTO.getNextEndTime())).toList();
        }

        return policyList;
    }
}
